<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>个人中心</span>
        </div>
      </template>
      
      <div class="profile-content">
        <!-- 用户基本信息 -->
        <div class="user-info-section">
          <el-avatar :size="80" :src="userInfo.avatar || ''">
            {{ userInfo.username?.substring(0, 1) }}
          </el-avatar>
          <div class="user-details">
            <h2>{{ userInfo.username }}</h2>
            <p class="user-role">{{ userInfo.isAdmin ? '管理员' : '普通用户' }}</p>
            <p class="user-phone" v-if="userInfo.phone">手机号：{{ userInfo.phone }}</p>
          </div>
        </div>
        
        <!-- 功能选项卡 -->
        <el-tabs v-model="activeTab" class="profile-tabs" @tab-change="handleTabChange">
          <el-tab-pane label="基本信息" name="basic">
            <div class="tab-content">
              <el-form :model="userInfo" label-width="100px">
                <el-form-item label="用户名">
                  <el-input v-model="userInfo.username" disabled />
                </el-form-item>
                <el-form-item label="手机号">
                  <el-input v-model="userInfo.phone" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updateUserInfo">保存修改</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="角色权限" name="roles">
            <div class="tab-content">
              <UserRoleInfo />
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="修改密码" name="password">
            <div class="tab-content">
              <el-form :model="passwordForm" label-width="100px" ref="passwordFormRef">
                <el-form-item label="原密码" prop="oldPassword">
                  <el-input 
                    v-model="passwordForm.oldPassword" 
                    type="password" 
                    show-password
                  />
                </el-form-item>
                <el-form-item label="新密码" prop="newPassword">
                  <el-input 
                    v-model="passwordForm.newPassword" 
                    type="password" 
                    show-password
                  />
                </el-form-item>
                <el-form-item label="确认密码" prop="confirmPassword">
                  <el-input 
                    v-model="passwordForm.confirmPassword" 
                    type="password" 
                    show-password
                  />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="changePassword">修改密码</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import UserRoleInfo from '@/components/UserRoleInfo.vue'
import { getCurrentUserInfo, updateUser, changePassword } from '@/api/user'

const route = useRoute()
const activeTab = ref('basic')
const passwordFormRef = ref()

const userInfo = reactive({
  id: '',
  username: '',
  phone: '',
  avatar: '',
  roles: [],
  isAdmin: false
})

const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    const response = await getCurrentUserInfo()
    if (response.code === 200) {
      Object.assign(userInfo, response.data)
    }
  } catch (err) {
    console.error('获取用户信息失败:', err)
    ElMessage.error('获取用户信息失败')
  }
}

// 更新用户信息
const updateUserInfo = async () => {
  try {
    const data = {
      id: userInfo.id,
      phone: userInfo.phone
    }
    
    const response = await updateUser(data)
    if (response.code === 200) {
      ElMessage.success('用户信息更新成功')
    } else {
      ElMessage.error(response.message || '更新失败')
    }
  } catch (err) {
    console.error('更新用户信息失败:', err)
    ElMessage.error('更新用户信息失败')
  }
}

// 修改密码
const changePassword = async () => {
  if (passwordForm.newPassword !== passwordForm.confirmPassword) {
    ElMessage.error('两次输入的密码不一致')
    return
  }
  
  if (passwordForm.newPassword.length < 6) {
    ElMessage.error('密码长度不能少于6位')
    return
  }
  
  try {
    const data = {
      oldPassword: passwordForm.oldPassword,
      newPassword: passwordForm.newPassword
    }
    
    const response = await changePassword(data)
    if (response.code === 200) {
      ElMessage.success('密码修改成功')
      
      // 重置表单
      passwordForm.oldPassword = ''
      passwordForm.newPassword = ''
      passwordForm.confirmPassword = ''
    } else {
      ElMessage.error(response.message || '密码修改失败')
    }
  } catch (err) {
    console.error('修改密码失败:', err)
    ElMessage.error('修改密码失败')
  }
}

// 处理标签页切换
const handleTabChange = (tabName) => {
  // 可以在这里处理标签页切换的逻辑
  console.log('切换到标签页:', tabName)
}

// 页面加载时获取用户信息
onMounted(() => {
  fetchUserInfo()
  
  // 检查路由参数，如果指定了标签页则切换到对应标签页
  if (route.query.tab) {
    activeTab.value = route.query.tab
  }
})
</script>

<style scoped>
.profile-container {
  max-width: 1000px;
  margin: 0 auto;
}

.profile-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  font-size: 18px;
}

.profile-content {
  padding: 20px 0;
}

.user-info-section {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
}

.user-details {
  margin-left: 20px;
}

.user-details h2 {
  margin: 0 0 10px 0;
  font-size: 24px;
  color: #303133;
}

.user-role {
  margin: 5px 0;
  color: #409eff;
  font-weight: 500;
}

.user-email {
  margin: 5px 0;
  color: #909399;
  font-size: 14px;
}

.profile-tabs {
  margin-top: 20px;
}

.tab-content {
  padding: 20px 0;
}
</style>